local U = require "packages/utility/utility"
local getAllCardNames = function(card_type, true_name)
    local all_names = {}
    local basics = {}
    local normalTricks = {}
    local delayedTricks = {}
    local equips = {}
    for _, card in ipairs(Fk.cards) do
        if card.package and not table.contains(Fk:currentRoom().disabled_packs, card.package.name) and not card.is_derived then
            if card.type == Card.TypeBasic then
                table.insertIfNeed(basics, true_name and card.trueName or card.name)
            elseif card.type == Card.TypeTrick and card.sub_type ~= Card.SubtypeDelayedTrick then
                table.insertIfNeed(normalTricks, true_name and card.trueName or card.name)
            elseif card.type == Card.TypeTrick and card.sub_type == Card.SubtypeDelayedTrick then
                table.insertIfNeed(delayedTricks, true_name and card.trueName or card.name)
            elseif card.type == Card.TypeEquip then
                table.insertIfNeed(equips, true_name and card.trueName or card.name)
            end
        end
    end
    if card_type:find("b") then
        table.insertTable(all_names, basics)
    end
    if card_type:find("t") then
        table.insertTable(all_names, normalTricks)
    end
    if card_type:find("d") then
        table.insertTable(all_names, delayedTricks)
    end
    if card_type:find("e") then
        table.insertTable(all_names, equips)
    end
    return all_names
end

local xingqi_global = fk.CreateSkill{
    name = "ym1__xingqi_global&",
}
xingqi_global:addEffect("viewas",{
    expand_pile = function (self,player)
        return player:getCardIds("j")
    end,
    pattern = ".|.|.|.|.|basic",
    interaction = function(self,player)
        local all_names = getAllCardNames("b")
        local names = player:getViewAsCardNames("ym1__xingqi", all_names)
        if #names > 0 then
          return UI.CardNameBox { choices = names, all_choices = all_names }
        end
    end,
    card_filter = function(self, player, to_select, selected)
        return #selected == 0 and table.contains(Self:getCardIds("ej"),to_select)
    end,
    view_as = function (self, player, cards)
        if #cards ~= 1 or not self.interaction.data then return end
        local card = Fk:cloneCard(self.interaction.data)
        card:addSubcard(cards[1])
        card.skillName = "ym1__xingqi"
        return card
    end,
    enabled_at_play = function (self, player)
        return #player:getCardIds("ej") > 0 and not player:hasSkill("ym1__xingqi")
    end,
    enabled_at_response = function (self, player)
        return #player:getCardIds("ej") > 0 and not player:hasSkill("ym1__xingqi")
    end,
    before_use = function (self, player, use)
        local room = player.room
        local card = use.card
        local currents = table.filter(room:getAllPlayers(),function (cp)
            return cp:hasSkill("ym1__xingqi")
        end)
        if card.color ~= Fk:getCardById(room.draw_pile[1]).color and #currents > 0 then
            room:moveCardTo(use.card, Card.DiscardPile, nil, fk.ReasonJustMove, "ym1__xingqi", nil, true, player.id)
            currents[1]:drawCards(1,"ym1__xingqi")
            return ""
        end
    end,
})

return xingqi_global